// pagesUser/coupon/index.js
let app = getApp();
Page({

    onShow() {
      // setTimeout(()=>{
      //   this.selectComponent("#page").hideLoading()
      // },2000)
      this.setData({
        allTabList:[],
        
      })
      this._requestPageList(true);

    },
    ...app.globalData.function,
    data: {
      ...app.globalData.config,
      triggered: false,   
      winHeight: "",// 窗口高度
      currentTab: 0, // 当前所在滑块的 index
      allTabList:[], // 全部    
      scrollTop: 0, // 设置竖向滚动条位置
      windowHeight: '',
      windowWidth: '',
      recordCount: 0, //列表总条数
      pageNo: 1, //页码
      totalPrice:0,
      all_check:0.00,
      like_list:[]
    },
  
  
    confirm(){
      let allTabList=this.data.allTabList
      let car_ids=[];
      for (let i = 0; i < allTabList.length; i++) {             
        for (let j = 0; j < allTabList[i].list.length; j++) {
          if(allTabList[i].list[j].checked){
            car_ids.push(allTabList[i].list[j].id)
          }
        }
      }
      let carIds=car_ids.toString()
      if(!carIds){
        this.$showToast('请选择商品信息！');
        return 
      } 
      this.router("/pagesOrder/confirm/index?carIds="+carIds+"&orderType=carts")
    },

    onLoad: function(options) {
      // 计算drawer高度，占满window
      let that = this;
      //  高度自适应
      wx.getSystemInfo({
        success: function (res) {
          let clientHeight = res.windowHeight,
            clientWidth = res.windowWidth,
            rpxR = 750 / clientWidth;
          let calc = (clientHeight - 50 - 54) * rpxR - 130;
          that.setData({
            windowHeight:  res.windowHeight,
            windowWidth:  res.windowWidth,
            winHeight: calc
          });
        }
      });
    },

    // 参数
    _requestPageList(isLoadMore = false, callback) {
      // isLoadMore 为true是上拉加载，不需要置空数组
      if(!isLoadMore){
        this.setData({
          allTabList:[], // 全部       
          pageNo: 1
        });
      }
      let opt = {
        pageNo :this.data.pageNo,
        token:this.getStorage('user_info').wx_openid,
      }
      
      this._requestPageListCom("virtualTaskList", opt, callback);
    },
    // 列表接口
    _requestPageListCom(url, params, callback) {    
      this.carList(params).then(res => {  
        
        if (res.resCode == "9999") {       
          let oldList = [];     
          oldList = this.data.allTabList;
          const newGoodsList = res.data.list
          const allData = [...oldList, ...newGoodsList]
          if(!res.data.list){
            this.setData({
              totalPrice:0.00,
              all_check:0,
            })
          }else{
            this.setData({
              recordCount:res.data.page,
              allTabList:allData,  
              like_list:res.data. like_list          
            })
          }          
          if(callback && typeof callback === 'function'){
            callback()
          }
        }
      })
    },

    
    
    // 上拉加载  滚动到底部/右边时触发
    allLoadMore(e){
      let _self = this;
      if (this._allLoadMore  || _self.data.recordCount === 0) return
      this._allLoadMore = true;
  
      this.setData({
        pageNo: ++_self.data.pageNo
      });
      this._requestPageList(true, () => {
        _self._allLoadMore = false;
      });
    },
    // 下拉刷新接口
    onAllRefresh() {
      if (this._allFreshing) return
      this._allFreshing = true
      this._requestPageList(false, () => {
        // 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新
        this.setData({
          triggered: false
        })
        this._allFreshing = false
      });
    },
    pScrollTo(){
      // 返回顶部
      this.setData({
        scrollTop:0
      });
    },

     //获取数量
     bindInputNumber(e){
      let num = 1     
      const index = e.currentTarget.dataset.carid
      let type=e.detail.type
      let storageData = this.getStorage('user_info')

      let obj_data={
        token:storageData.wx_openid,
        carId:index,
        number:num,
        type:type=='plus'?"1":"2",
      }
      this.caredit(obj_data).then(res=>{          
        if(res.resCode=="9999"){
          this.setData({
            allTabList:res.data.list
          })         
        }else{
          wx.showToast({title: res.resMessage,icon:"none"})
        }          
      })
    },

    //点击按钮
    radioChange(e){
        let index=e.currentTarget.dataset.index
        let type=e.currentTarget.dataset.type
        let allTabList=this.data.allTabList
        let is_check=false
        let totalPrice=0
        for (let i = 0; i < allTabList.length; i++) {         
          if(allTabList[i].storageId==index && type==1){            
            if( allTabList[i].checked){
              allTabList[i].checked=false;
              for (let j = 0; j < allTabList[i].list.length; j++) {
                allTabList[i].list[j].checked=false;
              }
            }else{
              allTabList[i].checked=true;
              for (let j = 0; j < allTabList[i].list.length; j++) {
                allTabList[i].list[j].checked=true;
              }
            }              
          }
            
          for (let j = 0; j < allTabList[i].list.length; j++) {            
            if(allTabList[i].list[j].id==index && type ==2){
              if(allTabList[i].list[j].checked){
                allTabList[i].list[j].checked=false
              }else{
                allTabList[i].list[j].checked=true
              }
            }           
          }           
        }
        
      
        for (let i = 0; i < allTabList.length; i++) {
          for (let j = 0; j < allTabList[i].list.length; j++) {
            if(allTabList[i].list[j].checked){
              is_check=true
              if(allTabList[i].storageId==allTabList[i].list[j].storageId) allTabList[i].checked=true
            }
            if(!is_check) if(allTabList[i].storageId==allTabList[i].list[j].storageId) allTabList[i].checked=false

            if(allTabList[i].list[j].checked){
              totalPrice += allTabList[i].list[j].goods_price * allTabList[i].list[j].goods_number;
            }
          }          
        }
        this.setData({
          allTabList:allTabList,
          totalPrice:totalPrice.toFixed(2)
        })        
    },

    //全选按钮
    checkAll(){
      if(this.data.all_check){
        this.setData({
          all_check:false
        })
      }else{
        this.setData({
          all_check:true
        })
      }
    },


    //全部操作
    checkAll(){
      let allTabList=this.data.allTabList
      let totalPrice=0
      if(!this.data.all_check){
        for (let i = 0; i < allTabList.length; i++) {  
          allTabList[i].checked=true;     
          for (let j = 0; j < allTabList[i].list.length; j++) {
            allTabList[i].list[j].checked=true;

            if(allTabList[i].list[j].checked){
              totalPrice += allTabList[i].list[j].goods_price * allTabList[i].list[j].goods_number;
            }

          }
        }
        this.setData({
          allTabList:allTabList,
          totalPrice:totalPrice.toFixed(2),
          all_check:true
        }) 
      }else{
        for (let i = 0; i < allTabList.length; i++) {  
          allTabList[i].checked=false;     
          for (let j = 0; j < allTabList[i].list.length; j++) {
            allTabList[i].list[j].checked=false;
          }
        }
        this.setData({
          allTabList:allTabList,
          totalPrice:0.00,
          all_check:false
        }) 
      }
    },

       // 商品详情
    goDetails:function(events){
      let meetId = events.currentTarget.dataset.posid;
      this.router("/pagesShop/detail/index?id="+meetId)
    },

})